package com.wyzz.hr.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.struts2.ServletActionContext;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.RoleBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.bo.impl.EmpDistinctNo;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.Pager;

public class UserListAction extends BaseAction {

	public UserListAction() {
		pager = new Pager();
	}

	public String execute() throws Exception {
		try {
			RoleBo roleService = (RoleBo) SpringBeanFactory
					.getBean("roleService");
			UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
			if (roleList == null)
				roleList = roleService.getRoleList();
			IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
					.getBean("departmentBO");
			departList = deptbo.FindEnabledDepartment();
			userList = userBo.searchUserList(empName, user, depart, manager,
					role, pager);
			password = RandomStringUtils.random(6, true, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String del() throws Exception {
		clearErrorsAndMessages();
		if (userDelId == null)
			return "success";
		UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
		user = userBo.getUserById(userDelId);
		if (user == null)
			return "error";
		HttpSession session = ServletActionContext.getRequest().getSession();
		String userNo = EmpDistinctNo.getEmpDistinctNo(userDelId).toUpperCase();
		if ("demo".equals(session.getAttribute("loginModel"))
				&& userNo.indexOf("TYHRM") != -1) {
			addActionError("TYHRMXXX\u662F\u53D7\u4FDD\u62A4\u7684\u7528\u6237\uFF0CDemo\u7248\u4E0D\u80FD\u64CD\u4F5C\uFF01");
			user = null;
			return "error";
		}
		Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
		if (user.getId() != null && userinfo.getId().equals(user.getId())) {
			addErrorInfo("\u6B64\u7528\u6237\u662F\u60A8\u672C\u4EBA\uFF0C\u4E0D\u80FD\u5220\u9664!");
			user = null;
			return "error";
		}
		Userinfo oldUserinfo = getUserBo().getUserById(user.getId());
		if (getUserBo().checkAuthModule(oldUserinfo, "911")
				&& getUserBo().getAdminCount("911") < 2) {
			addErrorInfo("\u5220\u9664\u5931\u8D25\uFF0C\u6B64\u7528\u6237\u4E3A\u552F\u4E00\u7684\u7CFB\u7EDF\u7BA1\u7406\u5458\uFF0C\u5220\u9664\u540E\u4F1A\u5BFC\u81F4\u7CFB\u7EDF\u65E0\u4EBA\u80FD\u7BA1\u7406\uFF01");
			user = null;
			return "error";
		}
		String name = EmpDistinctNo.getEmpName(userDelId);
		if (getUserBo().delUser(user.getId()))
			addSuccessInfo((new StringBuilder()).append(
					"\u5220\u9664\u7528\u6237(").append(name).append(
					")\u6210\u529F\u3002").toString());
		else
			addErrorInfo((new StringBuilder()).append(
					"\u5220\u9664\u7528\u6237(").append(name).append(
					")\u5931\u8D25!").toString());
		user = null;
		return "success";
	}

	public UserBo getUserBo() {
		if (userBo == null)
			userBo = (UserBo) SpringBeanFactory.getBean("userService");
		return userBo;
	}

	public List getUserList() {
		return userList;
	}

	public void setUserList(List userList) {
		this.userList = userList;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public List getDepartList() {
		return departList;
	}

	public void setDepartList(List departList) {
		this.departList = departList;
	}

	public String getDepart() {
		return depart;
	}

	public void setDepart(String depart) {
		this.depart = depart;
	}

	public String getManager() {
		return manager;
	}

	public void setManager(String manager) {
		this.manager = manager;
	}

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public int getRoleIndex() {
		if (role == null || role.equals("")) {
			return 0;
		} else {
			roleIndex = Integer.parseInt(role.trim());
			return roleIndex;
		}
	}

	public void setRoleIndex(int roleIndex) {
		if (role == null)
			this.roleIndex = 0;
		else
			this.roleIndex = Integer.parseInt(role.trim());
	}

	public String getUserDelId() {
		return userDelId;
	}

	public void setUserDelId(String userDelId) {
		this.userDelId = userDelId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private static final long serialVersionUID = 1L;
	private static final String AUTHMODULE = "911";
	private List userList;
	private List roleList;
	private List departList;
	private int roleIndex;
	private String role;
	private Userinfo user;
	private UserBo userBo;
	private String depart;
	private String manager;
	private String userDelId;
	private String empName;
	private String password;
	private Pager pager;
}
